package com.android.log;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.file.Config;
import com.android.file.FileHandler;
import com.android.file.RecordsPath;
import com.shushan.app_frame.R;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: LogActivity
 * @Desciption: crash本地日志界面
 * @author: yichaohua
 * @date: 2017-4-11
 */
public class LogActivity extends Activity {

	private static final String TAG = "LogActivity";

	// 用于格式化日期,日志文件名
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	private TextView tvLog;
	
	
	private View layoutLog;
	private View layoutPwd;
	
	private EditText etPwd;
	private ScrollView scrollView;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		setContentView(R.layout.screen_exception_log);

		registerComponent();
	}
	

	protected void registerComponent(){
		scrollView = (ScrollView)findViewById(R.id.scroll_view);
		
		layoutLog = findViewById(R.id.layout_log);
		tvLog = (TextView)findViewById(R.id.tv_log);
		layoutLog.setVisibility(View.GONE);
		
		layoutPwd = findViewById(R.id.layout_pwd);
		etPwd = (EditText)findViewById(R.id.et_pwd);
		etPwd.addTextChangedListener(mTextWatcher);
		
		
	}
	
	
	private void loadLog(){
		String date = formatter.format(new Date());
	    String path = RecordsPath.app_log + date + ".txt";
	    
		FileHandler handler = FileHandler.getInstance();
		InputStream input = handler.read(path);
		byte[] body = handler.readBytes(input);
		
		if(body != null){
			tvLog.setText(new String(body));
			
			Handler h = new Handler();
		    h.post(new Runnable() {
		        @Override
		        public void run() {  
		            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
		        }  
		    });  
		}
	}
	
	
	
	private TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			String key = etPwd.getText().toString();
			
			if(key.equals(Config.Log_Passwd)){
				layoutPwd.setVisibility(View.GONE);
				layoutLog.setVisibility(View.VISIBLE);
				
				hideInputPanel(etPwd);
				loadLog();
				
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
			// TODO Auto-generated method stub
		}  
	};
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			
	        return false;
		}
		
		return super.onKeyDown(keyCode, event);
		
	}

	/**
	 * 为特定EditText隐藏键盘
	 *
	 * @param editText
	 */
	public void hideInputPanel(EditText editText) {

		((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
